package com.party.service.vote.impl;

import com.party.common.Page;
import com.party.mapper.VoPtionMapper;
import com.party.mapper.VoteMapper;
import com.party.mapper.VotePersonMapper;
import com.party.model.*;
import com.party.service.vote.VoteService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class VoteServiceImpl implements VoteService {

    @Resource
    VoteMapper voteMapper;

    @Resource
    VotePersonMapper votePersonMapper;

    @Resource
    VoPtionMapper voPtionMapper;

    @Override
    public int addVote(Vote vote) {
        int num = voteMapper.insertSelective(vote);
        return num;
    }

    @Override
    public int deleteVoteByPrimaryKey(Integer id) {
        int num = voteMapper.deleteByPrimaryKey(id);
        return num;
    }

    public int deleteVoteByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                /**
                 * 删除投票
                 */
                num += voteMapper.deleteByPrimaryKey(Integer.parseInt(roid));

                /**
                 * 删除投票人
                 */
                VotePersonExample votePersonExample=new VotePersonExample();
                VotePersonExample.Criteria criteria=votePersonExample.createCriteria();
                criteria.andVoteidEqualTo(Integer.parseInt(roid));
                votePersonMapper.deleteByExample(votePersonExample);

                /**
                 * 删除被投票人
                 */
                VoPtionExample voPtionExample=new VoPtionExample();
                VoPtionExample.Criteria voPtionCriteria=voPtionExample.createCriteria();
                voPtionCriteria.andVoteidEqualTo(Integer.parseInt(roid));
                voPtionMapper.deleteByExample(voPtionExample);
            }
        }
        return num;
    }

    @Override
    public Vote getVoteByPrimaryKey(Integer id) {
        Vote vote = voteMapper.selectByPrimaryKey(id);
        return vote;
    }

    @Override
    public int updateVoteByPrimaryKey(Vote Vote) {
        int num = voteMapper.updateByPrimaryKeySelective(Vote);
        return num;
    }

    @Override
    public  List<Vote> getAllVote() {
        VoteExample voteExample = new VoteExample();
        List<Vote> listVote=voteMapper.selectByExample(voteExample);
        return listVote;
    }

    @Override
    public Page getVoteList(Vote vote, int pageSize, int pageNumber) {
        VoteExample voteExample = new VoteExample();
        /**
         * 可以添加查询条件
         */
        if(vote != null)
        {
            VoteExample.Criteria criteria = voteExample.createCriteria();
            if(vote.getTitle() != null && !"".equals(vote.getTitle()))
            {
                criteria.andTitleLike("%"+vote.getTitle()+"%");
            }
            if(vote.getCreattime() != null)
            {
                criteria.andCreattimeGreaterThanOrEqualTo(vote.getCreattime());
            }
        }
        voteExample.setOrderByClause("creatTime desc");

        //获取总的记录数量
        int count = voteMapper.countByExample(voteExample);
        //获取指定的页码数据
        voteExample.setBeginNum((pageNumber - 1) * pageSize);
        voteExample.setEndNum(pageSize);
        List<Vote> list = voteMapper.getListPage(voteExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

}
